package com.itheima.brand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.brand.mapper.BrandMapper;
import com.itheima.brand.service.BrandService;
import com.itheima.goods.pojo.Brand;
import com.sun.org.apache.bcel.internal.generic.NEW;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

//    @Autowired
//    private BrandMapper brandMapper;

    @Override
    public Result findAll() {
//        List<Brand> brandList = brandMapper.findAll();
//        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
//        wrapper.orderByAsc(Brand::getId);
//        IPage<Brand> page = new Page<>(1, 10);
//        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
//        wrapper.orderByAsc(Brand::getId);
//        this.page(page, wrapper);
//        List<Brand> brandList = page.getRecords();
//        System.out.println(brandList);
//        PageResult<Brand> pageResult = new PageResult<>();
//        long total = page.getTotal();
//        pageResult.setTotal(total);
//        pageResult.setRows(brandList);

        List<Brand> brandList = this.list();

        if (brandList == null) {
            return new Result(false, StatusCode.ERROR, "查询失败", null);
        }
        return new Result(true, StatusCode.OK, "查询所有成功", brandList);
    }

    @Override
    public Result add(Brand brand) {

        boolean b = this.save(brand);
        if (b) {
            new Result(false, StatusCode.ERROR, "添加失败", null);

        }

        return new Result(true, StatusCode.OK, "添加成功", null);
    }

    @Override
    public Result findPage(Integer page, Integer size) {
        Page<Brand> brandPage = new Page<>(page, size);
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Brand::getId);
        this.page(brandPage, wrapper);
        List<Brand> brandList = brandPage.getRecords();
        PageResult<Brand> pageResult = new PageResult<>();
        Long total = pageResult.getTotal();
        pageResult.setTotal(total);
        pageResult.setRows(brandList);
        return new Result(true, StatusCode.OK, "分页查询", pageResult);
    }

    @Override
    public Result findById(Integer id) {

        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Brand::getId, id);
        Brand brand = this.getById(id);

        return new Result(true, StatusCode.OK, "查询成功", brand);
    }

    @Override
    public Result updateBrand(Brand brand, Integer id) {
        LambdaUpdateWrapper<Brand> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Brand::getId, id);
        boolean b = this.update(brand, wrapper);
        System.out.println(b);
        if (!b) {
            return new Result(false, StatusCode.ERROR, "更改失败", null);

        }

        return new Result(true, StatusCode.OK, "更改成功", null);
    }

    @Override
    public Result deleteBrand(Integer id) {
        boolean b = this.removeById(id);
        if (!b) {
            new Result(false, StatusCode.ERROR, "删除失败", null);

        }

        return new Result(true, StatusCode.OK, "删除成功", null);
    }

    @Override
    public Result findPage2(Integer page, Integer size, Brand brand) {
        Page<Brand> brandPage = new Page<>(page,size);
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        if (brand.getName() != null && !brand.getName().trim().equals("")) {
            wrapper.like(Brand::getName, brand.getName());
        }
        if (brand.getImage() != null && !brand.getImage().trim().equals("")) {
            wrapper.eq(Brand::getImage, brand.getImage());
        }
        if (brand.getLetter() != null && !brand.getLetter().trim().equals("")) {
            wrapper.eq(Brand::getLetter, brand.getLetter());
        }
        if (brand.getSeq() != null) {
            wrapper.eq(Brand::getSeq, brand.getSeq());
        }
        wrapper.orderByAsc(Brand::getId);
        this.page(brandPage, wrapper);
        List<Brand> brandList = brandPage.getRecords();

        PageResult<Brand> pageResult = new PageResult<>();
        Long total = pageResult.getTotal();
        pageResult.setTotal(total);
        pageResult.setRows(brandList);


        System.out.println("brandList = " + brandList);
        if (brandList == null) {
            return new Result(false, StatusCode.ERROR, "查询失败", null);
        }

        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }
}





